import {createSlice} from "@reduxjs/toolkit";
import {
    getApplicantCountAPI,
    getApplicantCountByNameAPI,
    getApplicantInfoAPI,
    getApplicantListAPI,
    getApplicantListByNameAPI
} from "@/apis/applicant.jsx";


const applicantStore=createSlice({
    name:'applicant',
    initialState:{
        applicantInfo_init:{},
        applicantList_init:[],
        applicantCount_init:0,
        applicantCur_init:1,
        applicantPageSize_init:11,
        applicantDriveListChange_init:0
    },
//     同步方法
    reducers:{
        getApplicantInfo(state,action){
            state.applicantInfo_init=action.payload
        },
        getApplicantList(state,action){
            state.applicantList_init=action.payload
        },
        getApplicantCount(state,action){
            state.applicantCount_init=action.payload
        },
        setApplicantCur(state,action){
            state.applicantCur_init=action.payload;
        },
        setApplicantPagerSize(state,action){
            state.applicantPageSize_init=action.payload;
        },
        // 定义一个状态数据，用于监测选中了那条记录，参数是选中对象中的id,返回值就是这个id值
        setApplicantDriveListChange(state,action){
            state.applicantDriveListChange_init=action.payload;
        }
    }
})
const {
    setApplicantDriveListChange
    ,getApplicantInfo
    ,getApplicantList
    ,getApplicantCount
    ,setApplicantCur
    ,setApplicantPagerSize}=applicantStore.actions;
// 异步请求函数
const fetchGetApplicantList=(cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getApplicantListAPI(cur,pageSize);
        dispatch(getApplicantList(res.data))
    }
}
const fetchGetApplicantInfo=(id)=>{
    return async (dispatch)=>{
        const res=await getApplicantInfoAPI(id);
        dispatch(getApplicantInfo(res.data))
    }
}
const fetchGetApplicantCount=()=>{
    return async (dispatch)=>{
        const res=await getApplicantCountAPI();
        dispatch(getApplicantCount(res.data))
    }
}
const fetchGetApplicantListByName=(name,cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getApplicantListByNameAPI(name,cur,pageSize);
        dispatch(getApplicantList(res.data))
    }
}
const fetchGetApplicantCountByName=(name)=>{
    return async (dispatch)=>{
        const res=await getApplicantCountByNameAPI(name);
        dispatch(getApplicantCount(res.data))
    }
}

const applicantReducer=applicantStore.reducer;

export {
    fetchGetApplicantList,
    fetchGetApplicantInfo,
    fetchGetApplicantCount,
    fetchGetApplicantListByName,
    fetchGetApplicantCountByName,
    setApplicantCur,
    setApplicantPagerSize,
    setApplicantDriveListChange,
}


export default applicantReducer;